﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Yipusoft.Evaluate.DBUtility;
using Yipusoft.Evaluate.Model;

namespace Yipusoft.Evaluate.DAL
{
    public class AwardItem
    {
        public bool Exsits(int ItemID)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@ItemID",SqlDbType.Int)
            };
            parameters[0].Value = ItemID;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "AwardItems_Exsits", parameters);
                if (dr.Read())
                    return true;
                else
                    return false;
            }
        }
        public int Insert(AwardItemInfo AwardItem)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@ItemID",SqlDbType.Int),
                new SqlParameter("@AwardID",SqlDbType.Int),
                new SqlParameter("@DepartmentID",SqlDbType.NVarChar,50),
                new SqlParameter("@ItemName",SqlDbType.NVarChar,50),
                new SqlParameter("@ItemScore",SqlDbType.Decimal),
                new SqlParameter("@Description",SqlDbType.NVarChar,500)
            };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = AwardItem.AwardID;
            parameters[2].Value = AwardItem.DepartmentID;
            parameters[3].Value = AwardItem.ItemName;
            parameters[4].Value = AwardItem.ItemScore;
            parameters[5].Value = AwardItem.Description;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "AwardItems_Insert", parameters);
                return (int)parameters[0].Value;
            }
        }
        public bool Update(AwardItemInfo AwardItem)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@ItemID",SqlDbType.Int),
                new SqlParameter("@AwardID",SqlDbType.Int),
                new SqlParameter("@DepartmentID",SqlDbType.NVarChar,50),
                new SqlParameter("@ItemName",SqlDbType.NVarChar,50),
                new SqlParameter("@ItemScore",SqlDbType.Decimal),
                new SqlParameter("@Description",SqlDbType.NVarChar,500)
            };
            parameters[0].Value = AwardItem.ItemID;
            parameters[1].Value = AwardItem.AwardID;
            parameters[2].Value = AwardItem.DepartmentID;
            parameters[3].Value = AwardItem.ItemName;
            parameters[4].Value = AwardItem.ItemScore;
            parameters[5].Value = AwardItem.Description;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "AwardItems_Update", parameters);
                return rowAffected > 0;
            }
        }
        public bool Delete(int ItemID)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@ItemID",SqlDbType.Int)
            };
            parameters[0].Value = ItemID;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "AwardItems_Delete", parameters);
                return rowAffected > 0;
            }
        }
        public AwardItemInfo GetAwardItem(int ItemID)
        {
            AwardItemInfo AwardItem = null;
            SqlParameter[] parameters = {
                new SqlParameter("@ItemID",SqlDbType.Int)
            };
            parameters[0].Value = ItemID;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "AwardItems_GetDetail", parameters);
                if (dr.Read())
                {
                    AwardItem = new AwardItemInfo();
                    AwardItem.ItemID = dr.GetInt32(0);
                    AwardItem.AwardID = dr.GetInt32(1);
                    AwardItem.DepartmentID = dr.GetString(2);
                    AwardItem.ItemName = dr.GetString(3);
                    AwardItem.ItemScore = dr.GetDecimal(4);
                    AwardItem.Description = dr.GetString(5);
                }
                return AwardItem;
            }
        }
        public List<AwardItemInfo> GetList(string StrWhere)
        {
            List<AwardItemInfo> AwardItems = new List<AwardItemInfo>();
            AwardItemInfo AwardItem = null;
            SqlParameter[] parameters = {
                new SqlParameter("@StrWhere",SqlDbType.NVarChar,4000)
            };
            parameters[0].Value = StrWhere;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "AwardItems_GetList", parameters);
                while (dr.Read())
                {
                    AwardItem = new AwardItemInfo();
                    AwardItem.ItemID = dr.GetInt32(0);
                    AwardItem.AwardID = dr.GetInt32(1);
                    AwardItem.DepartmentID = dr.GetString(2);
                    AwardItem.ItemName = dr.GetString(3);
                    AwardItem.ItemScore = dr.GetDecimal(4);
                    AwardItem.Description = dr.GetString(5);
                    AwardItems.Add(AwardItem);
                }
                return AwardItems;
            }
        }
    }
}
